package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.constant.header.WasteListEnum;
import com.smartfactory.sail.entity.Waste;
import com.smartfactory.sail.entity.enums.WasteStatusEnum;
import com.smartfactory.sail.mapper.StorageCurrentInfoMapper;
import com.smartfactory.sail.service.IWasteService;
import com.smartfactory.sail.utils.CodeUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.waste.ProcessReq;
import com.smartfactory.sail.vo.waste.WasteList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 废品库 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-27
 */
@RestController
@RequestMapping("/waste")
@Api(tags ="废品")
public class WasteController extends ControllerResponse {

    private IWasteService iWasteService;
    private StorageCurrentInfoMapper storageCurrentInfoMapper;

    public WasteController(IWasteService iWasteService, StorageCurrentInfoMapper storageCurrentInfoMapper) {
        this.iWasteService = iWasteService;
        this.storageCurrentInfoMapper = storageCurrentInfoMapper;
    }

    @HeaderWrapper(WasteListEnum.class)
    @ApiOperation(value = "废品库列表", notes = "废品库列表")
    @PostMapping("list/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<WasteList>> list(@RequestBody Map<String, Object> queryParam,
                                              @PathVariable("currentPage") Long currentPage,
                                              @PathVariable("size") Integer size,
                                              @PathVariable("sortType") Integer sortType,
                                              @PathVariable("sortField") String sortField) {

        PageParam pageParam = new PageParam(currentPage, size, sortType, sortField);
        return ok(iWasteService.listBypage(pageParam, queryParam));
    }

    @ApiOperation(value = "根据id获取详情", notes = "根据id获取详情")
    @GetMapping("info/{id}")
    public ApiResponse<Waste> list(@PathVariable("id") Long id) {
        return ok(iWasteService.getById(id));
    }

    @ApiOperation(value = "提交报废", notes = "提交报废")
    @PostMapping("add")
    public ApiResponse<Long> add(@RequestBody Waste waste) {
        waste.setCode(CodeUtils.creatFPCode());
        if (iWasteService.save(waste)) {
            return ok(waste.getId());
        }
        return fail("保存失败");
    }


    @OperRecord(content = "处理报废",level = 1)
    @ApiOperation(value = "处理报废 processType=1 变卖2入原料库", notes = "处理报废 processType=1 变卖2入原料库")
    @PostMapping("process")
    public ApiResponse<Object> process(@RequestBody List<ProcessReq> req) {

        List<Waste> list = new ArrayList<>();

        for (ProcessReq processReq : req) {
            Long id = processReq.getId();
            Integer processType = processReq.getProcessType();
            BigDecimal price = processReq.getPrice();
            Integer size = processReq.getSize();
            Waste byId = iWasteService.getById(id);
            byId.setProcessType(processType);
            BeanUtils.copyProperties(processReq, byId);
            byId.setStatus(WasteStatusEnum.YCL);
            byId.setProcessSize(size);
            if (processType == 2) {
                //入原料库
                storageCurrentInfoMapper.updateMaterialSize(byId.getItemId(), size);
            } else {
                byId.setPrice(price);
            }
            list.add(byId);
        }
        if (iWasteService.updateBatchById(list)) {
            return ok("处理成功!");
        }

        return fail("处理失败");
    }

    @GetMapping("toBeProcessedList")
    @ApiOperation(value = "获取所有待处理列表",notes = "获取所有待处理列表")
    public ApiResponse<List<Waste>> toBeProcessed() {
        QueryWrapper<Waste> query = new QueryWrapper<>();
        query.eq("status", 1).select("id","code","itemName","custName","size");
        return ok(iWasteService.list(query));
    }

    @GetMapping("processedList")
    @ApiOperation(value = "获取所有已处理列表",notes = "获取所有已处理列表")
    public ApiResponse<List<Waste>> processedList() {
        QueryWrapper<Waste> query = new QueryWrapper<>();
        query.eq("status", 2).orderByDesc("updateTime");
        return ok(iWasteService.list(query));
    }
}
